﻿use CrmContacto


if not exists (select 1 from sys.objects where object_id = object_id('Sucursal'))
begin

CREATE TABLE [dbo].[Sucursal](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nombre] [varchar](50) NOT NULL,
	[CodigoExterno] [varchar](50) NULL,
	[EmpresaId] [int] NOT NULL,
	[AuditFechaCreacion] [datetime] NOT NULL,
	[AuditUserNameCreacion] [varchar](300) NOT NULL,
	[AuditFechaModificacion] [datetime] NULL,
	[AuditUserNameModificacion] [varchar](300) NULL,
	[Removed] [bit] NOT NULL,
 CONSTRAINT [PK_Sucursal] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

end

GO

if not exists (select 1 from sys.objects where name = 'FK_Sucursal_Empresa' and parent_object_id = object_id('Sucursal'))
begin
alter table Sucursal add constraint FK_Sucursal_Empresa foreign key (EmpresaId) references Empresa(Id)
end 



GO

if not exists (select 1 from sys.columns where name = 'CodigoExterno' and object_id = object_id('PuntoDeVenta'))
begin
alter table PuntoDeVenta add CodigoExterno varchar(50) null
end

GO

if not exists (select 1 from sys.columns where name = 'SucursalId' and object_id = object_id('PuntoDeVenta'))
begin
alter table PuntoDeVenta add SucursalId int null
end

GO

if not exists (select 1 from sys.objects where name = 'FK_PuntoDeVenta_Sucursal' and parent_object_id = object_id('PuntoDeVenta'))
begin
alter table PuntoDeVenta add constraint FK_PuntoDeVenta_Sucursal foreign key (SucursalId) references Sucursal(Id)
end 



